﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace YXShop.Web.membercenter.individualinfo
{
    public partial class changevolume : System.Web.UI.Page
    {
        YXShop.BLL.Member.MemberAccount accountBll = new YXShop.BLL.Member.MemberAccount();
        YXShop.BLL.Member.MemberRank rankBll = new YXShop.BLL.Member.MemberRank();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["type"]==null)
            {
                ViewState["type"] = "1";
            }          
            int uid = ChangeHope.WebPage.PageRequest.GetQueryInt("uid");
            ViewState["uid"] = uid.ToString();
            YXShop.Model.Member.MemberAccount model = accountBll.GetModel(uid);
            if (model != null)
            {
                this.lblName.Text = model.UserId;
                this.lblCapital.Text = model.Capital.ToString();
                this.lblCoupons.Text = model.Coupons.ToString();
                this.lblPoints.Text = model.Points.ToString();
                this.lblGroup.Text = rankBll.GetModel(Convert.ToInt32(model.UserGroup)).Name.ToString();
                TimeSpan oldtime = new TimeSpan(Convert.ToDateTime(model.PeriodOfValidity).Ticks);
                TimeSpan newtime = new TimeSpan(Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).Ticks);
                TimeSpan gap = oldtime.Subtract(newtime).Duration();
                this.lblDay.Text = gap.Days.ToString();
            }
        }

        protected void radfund_CheckedChanged(object sender, EventArgs e)
        {
            ViewState["type"] = "1";
        }

        protected void radintegral_CheckedChanged(object sender, EventArgs e)
        {
            ViewState["type"] = "2";
        }

        #region 兑换积分
        protected void btnChange_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Member.UserInfoNote noteBll = new YXShop.BLL.Member.UserInfoNote();
            YXShop.Model.Member.UserInfoNote noteModel = new YXShop.Model.Member.UserInfoNote();
            YXShop.Model.Member.MemberAccount account = accountBll.GetModel(Convert.ToInt32(ViewState["uid"]));
            noteModel.NoteName = account.UserId;
            noteModel.NoteDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
            //资金
            if (ViewState["type"].ToString() == "1")
            {
                if (this.txtFundwrap.Text.Trim().Equals(""))
                {
                    this.txtFundwrapTip.CssClass = "actionErr";
                    this.txtIntewrapTip.CssClass = "msgNormal";
                    return;
                }
                else
                {
                    this.txtFundwrapTip.CssClass = "msgNormal";
                }
                if (!Isnum(this.txtFundwrap.Text.Trim()))
                {
                    this.txtFundwrapTip.CssClass = "actionErr";
                    this.txtIntewrapTip.CssClass = "msgNormal";
                    return;
                }
                else
                {
                    this.txtFundwrapTip.CssClass = "msgNormal";
                }
                if (Convert.ToDecimal(account.Capital) > Convert.ToDecimal(this.txtFundwrap.Text))
                {
                    account.Capital = Convert.ToDecimal(account.Capital) - Convert.ToDecimal(this.txtFundwrap.Text);
                    decimal wrap = Convert.ToDecimal(this.txtFundwrap.Text) * 15;
                    if (wrap > 0)
                    {
                        account.Coupons = Convert.ToDecimal(account.Coupons)+wrap;
                        accountBll.Update(account);
                        noteModel.UserID = Convert.ToInt32(account.UID);
                        noteModel.Username = account.UserId;
                        noteModel.Causation = "用于兑换点卷" + wrap + "点，消费" + this.txtFundwrap.Text + "元";
                        noteModel.BosomNote = "资金兑换点卷";
                        //记录点卷
                        noteModel.BuckleOrAdd = 0;
                        noteModel.NoteType = 0;
                        noteModel.TicketCount = Convert.ToDecimal(wrap);
                        noteBll.Add(noteModel);
                        //记录资金支出
                        noteModel.BuckleOrAdd = 1;
                        noteModel.NoteType = 1;
                        noteModel.TicketCount = Convert.ToDecimal(this.txtFundwrap.Text);
                        noteBll.Add(noteModel);
                        this.ltlMsg.Text = "资金兑换点卷成功";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";
                        return;
                    }
                    else
                    {
                        this.ltlMsg.Text = "你兑换的点卷数少于1，请输入正确的兑换值";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }

                }
                else
                {
                    this.ltlMsg.Text = "你帐户金额小于支付的金额，请您冲值。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            //积分
            if (ViewState["type"].ToString() == "2")
            {
                if (this.txtIntewrap.Text.Trim().Equals(""))
                {
                    this.txtIntewrapTip.CssClass = "actionErr";
                    this.txtFundwrapTip.CssClass = "msgNormal";
                    return;
                }
                else
                {
                    this.txtIntewrapTip.CssClass = "msgNormal";
                }
                if (!Isnum(this.txtIntewrap.Text.Trim()))
                {
                    this.txtIntewrapTip.CssClass = "actionErr";
                    this.txtFundwrapTip.CssClass = "msgNormal";
                    return;
                }
                else
                {
                    this.txtIntewrapTip.CssClass = "msgNormal";
                }
                if (Convert.ToDouble(this.txtIntewrap.Text) % 10 != 0)
                {

                    this.ltlMsg.Text = "您输入的积分数必须是兑换积分数的倍数";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }

                if (Convert.ToDecimal(account.Points) > Convert.ToDecimal(this.txtIntewrap.Text))
                {
                    account.Points = Convert.ToDecimal(account.Points) - Convert.ToDecimal(this.txtIntewrap.Text);
                    decimal wrap = Convert.ToDecimal(this.txtIntewrap.Text) / 10;
                    if (wrap >= 1)
                    {
                        account.Coupons = Convert.ToDecimal(account.Coupons) + wrap;
                        accountBll.Update(account);
                        noteModel.UserID = Convert.ToInt32(account.UID);
                        noteModel.Username = account.UserId;
                        noteModel.Causation = "用于兑换点卷" + wrap + "点，消费" + this.txtIntewrap.Text + "积分";
                        noteModel.BosomNote = "积分兑换点卷";
                        //记录有效期
                        noteModel.BuckleOrAdd = 0;
                        noteModel.NoteType = 0;
                        noteModel.TicketCount = Convert.ToDecimal(wrap);
                        noteBll.Add(noteModel);
                        //记录积分记录
                        YXShop.BLL.Member.Integral inteBll = new YXShop.BLL.Member.Integral();
                        YXShop.Model.Member.Integral inteModel = new YXShop.Model.Member.Integral();
                        inteModel.IntegralClass = 3;
                        inteModel.IntegralNum = Convert.ToDecimal(this.txtIntewrap.Text);
                        inteModel.IntegralStatus = 1;
                        inteModel.NoteDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
                        inteModel.NoteName = "系统兑换";
                        inteModel.Origin = "积分兑换点卷";
                        inteModel.Remark = "用于兑换点卷" + wrap + "点，消费" + this.txtIntewrap.Text + "积分";
                        inteModel.Userid = account.UID;
                        inteModel.GainDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
                        inteBll.Add(inteModel);

                        this.ltlMsg.Text = "积分兑换点卷成功";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";
                        return;

                    }
                    else
                    {
                        this.ltlMsg.Text = "你输入的积分小于兑换点卷积分。";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                else
                {
                    this.ltlMsg.Text = "你帐户积分小于支付的积分。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }

            }
        }
        #endregion

        #region 判断
        public bool Isnum(string values)
        {
            if (values != null && values != "")
            {
                string pattern = @"^(0?|[1-9]\d*)(\.\d{0,2})?$";
                return Regex.IsMatch(values, pattern);
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
